// index page
// ============

define(['require', 'zepto', 'utils', 'wx', 'ejs', 'sui'], function(require, $, utils, wx, ejs) {
  return {
    init: function(data) {
      this.unionid=data.unionid
      utils.configWeixin(data.jsConfig, false);
      this.requestData(data);
      this.headerHtml = '<a href="javascript:void(0);" class="item-content shopping-content">\
          <% if(product.id != 1) {%>\
          <div class="item-media" style="<%= product.single_avatar == true ? "width: 6.8rem; display: block;" : "" %>">\
            <% if(product.single_avatar == false) {%>\
            <img src="'+window.__CTX__.resource+'<%=product.coach_avatar%>.jpg?v=1.1.1" style="width: 6.8rem;">\
            <% } else { %>\
             <% product.coach_avatar_list.forEach(function(imageItem){%>\
               <img src="'+window.__CTX__.resource+'<%=imageItem%>.jpg?v=1.1.1" style="width: 6rem; margin-bottom: .3rem;">\
             <% }) %>\
             <% } %>\
          </div>\
          <% } %>\
      <div class="item-inner <%=product.id == 1 ? "no-pic" : "" %>" style="margin-left: .5rem;">\
      <div class="item-title-row">\
      <div class="item-title"><%=product.name%></div>\
      </div>\
      <div class="item-subtitle">\
      <div class="">课程导师：\
      <% product.coach_name_list.forEach(function(name, index){%>\
      <%= index == 0 ? "" : "&" %> <%=name%> \
      <% }) %>\
      </div>\
      <div class="">上课时间：<%=product.occur_time%></div>\
      </div>\
      <div class="item-text" style="height: 3rem;">\
        <div><%=product.id == 1 ? "精英" : "" %>装备包</div>\
        <div><%=product.equipment_package%></div>\
      </div>\
    </div>\
  </a>\
</div>\
<div class="black-block"></div>\
<div class="details-layout">\
  <p>姓名：<%=order.name%></p>\
  <p>手机号：<%=user.mobile%></p>\
  <p>性别：<%=order.gender%></p>\
  <p>年龄：<%=order.age%></p>\
  <br>\
  <p>短袖尺码：<%=order.clothes_size%></p>\
  <% if(product.id == 1) {%>\
  <p>鞋子尺码：<%=order.shoe_size%></p>\
  <% } %>\
  <br>\
  <p>下单时间：<%=order.order_time%></p>\
  <p>订单金额：￥<%=order.price / 100%></p>\
  <p>订单编号：<%=order.code%></p>\
</div>\
</div>\
</div>';
      
    },
    
    requestData: function(data) {
      var that = this;
      $.ajax({
        type : 'GET',
        url : window.__CTX__.domain + '/api/orders/' + data.order + "?unionid=" + that.unionid,
        dataType : 'json',
        timeout : 30000,
        success : function(res) {
          console.log(res);
          if(res.code == 0) {
              that.product = res.data.product
              that.order = res.data.order
              that.user = res.data.user,
              that.userInfo = res.data.userInfo
              var headerResult = ejs.render(that.headerHtml, {product : that.product, order: that.order, user: that.user, userInfo: that.userInfo})
              $('.product-info').html(headerResult);
          } else {
            $.toast("获取订单数据失败");
          } 
        },
        error : function(xhr, type) {
          alert('Ajax error!')
        }
      })
    }
  }
})